/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：ClientDiscussListServlet   
 * 类描述：   我的评论集合接口
 * 创建人：zhanghl   
 * 创建时间：2016年10月10日 下午3:28:17   
 * 修改人：zhanghl   
 * 修改时间：2016年10月10日 下午3:28:17   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientDiscussReqPara;
import com.lst.entity.response.ClientDiscussResPara;
import com.lst.model.ClientNum;
import com.lst.model.MstDiscuss;
import com.lst.service.ClientNumService;
import com.lst.service.MstDiscussService;
import com.lst.utils.CommFunUtils;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ClientDiscussListServlet
 * @Description: 我的评论集合接口
 * @author zhanghl
 * @date 2016年10月10日 下午3:28:17
 *
 */
public class ClientDiscussListServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private MstDiscussService mstDiscussService = ctx.getBean(MstDiscussService.class); 

	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		ClientDiscussResPara resPara = new ClientDiscussResPara();

		try {
			ClientDiscussReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer clientid = Integer.valueOf(reqPara.getUserid());
				Integer page = Integer.valueOf(reqPara.getPage());
				Integer pagesize = Integer.valueOf(reqPara.getPagesize());

				Map<String, Object> cdMap = new HashMap<String, Object>();

				cdMap.put("enabled", true);
				cdMap.put("isDeleted", false);
				cdMap.put("clientId", clientid);
				cdMap.put("orderByClause", "md.createdate desc");

				PageBounds pageBounds = new PageBounds(page , pagesize);

				PageBounds pagebounds = new PageBounds();

				//返回结果（我的通知）
				List<MstDiscuss> mdList = new ArrayList<MstDiscuss>();

				List<MstDiscuss> mdlist = mstDiscussService.queryList(cdMap, pageBounds);

				Map<String, Object> mymap = new HashMap<String, Object>();
				
				mymap.put("enabled", true);
				mymap.put("isDeleted", false);

				for(MstDiscuss md: mdlist){
					//主评论
					if(StringUtils.isEmpty(md.getParentid())){						

						//点赞量
						Integer praisenum = CommFunUtils.opNum(null, md.getId(), CommCode.SYSTEM_CARESEL_RANGE_DISCUSS, CommCode.SYSTEM_COUNT_PRAISE, clientNumService);

						md.setPraisenum(praisenum);

						Map<String, Object> temp = new HashMap<String, Object>();

						temp.put("enabled", true);
						temp.put("isDeleted", false);
						temp.put("rootId", md.getId());
						temp.put("tarId", md.getTarid());
						temp.put("type", md.getType());
						temp.put("parentId", "auxi");
						temp.put("orderByClause", "md.createdate desc");

						List<MstDiscuss> autoList = mstDiscussService.queryByList(temp, pagebounds);

						md.setMds(autoList);

						List<ClientNum> cns = null;

						if(!StringUtils.isEmpty(clientid)){
							Map<String, Object> map = new HashMap<String, Object>();

							map.put("enabled", true);
							map.put("isDeleted", false);
							map.put("clientid", Integer.valueOf(clientid));
							map.put("actid", md.getId());
							map.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_DISCUSS);
							map.put("numtype",CommCode.SYSTEM_COUNT_PRAISE);

							cns = clientNumService.queryByList(map);
						}

						if(null==cns||cns.size()==0){
							md.setIsSup(false);					
						}else{
							md.setIsSup(true);					
						}	

						mdList.add(md);
					}else{//次级评论
						mymap.put("parentId", "main");
						mymap.put("tarId", md.getTarid());
						mymap.put("type", md.getType());
						mymap.put("rootId", md.getRootid());
						
						List<MstDiscuss> main = mstDiscussService.queryByList(cdMap, pagebounds);
						
						//点赞量
						Integer praisenum = CommFunUtils.opNum(null, main.get(0).getId(), CommCode.SYSTEM_CARESEL_RANGE_DISCUSS, CommCode.SYSTEM_COUNT_PRAISE, clientNumService);

						main.get(0).setPraisenum(praisenum);

						Map<String, Object> tempmap = new HashMap<String, Object>();

						tempmap.put("enabled", true);
						tempmap.put("isDeleted", false);
						tempmap.put("rootId", main.get(0).getId());
						tempmap.put("tarId", main.get(0).getTarid());
						tempmap.put("type", main.get(0).getType());
						tempmap.put("parentId", "auxi");
						tempmap.put("orderByClause", "md.createdate desc");

						List<MstDiscuss> autoList = mstDiscussService.queryByList(tempmap, pagebounds);

						main.get(0).setMds(autoList);

						List<ClientNum> cns1 = null;

						if(!StringUtils.isEmpty(clientid)){
							Map<String, Object> map1 = new HashMap<String, Object>();

							map1.put("enabled", true);
							map1.put("isDeleted", false);
							map1.put("clientid", Integer.valueOf(clientid));
							map1.put("actid", main.get(0).getId());
							map1.put("acttype", CommCode.SYSTEM_CARESEL_RANGE_DISCUSS);
							map1.put("numtype",CommCode.SYSTEM_COUNT_PRAISE);

							cns1 = clientNumService.queryByList(map1);
						}

						if(null==cns1||cns1.size()==0){
							main.get(0).setIsSup(false);					
						}else{
							main.get(0).setIsSup(true);					
						}
						mdList.add(main.get(0));
					}

				}	
				resPara.setMdlist(mdList);
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

		//	logger.info("ClientDiscussListServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientDiscussReqPara getReqPara(HttpServletRequest request) {

		ClientDiscussReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ClientDiscussReqPara());

			reqPara = (ClientDiscussReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");

//			logger.info("reqPara userid: " + userid);
//			logger.info("reqPara page: " +page);
//			logger.info("reqPara pagesize: " +pagesize);

			reqPara.setUserid(userid);
			reqPara.setPage(page);
			reqPara.setPagesize(pagesize);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}
		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientDiscussReqPara cdReqPara = (ClientDiscussReqPara) reqPara;
		ClientDiscussResPara cdResPara = (ClientDiscussResPara) resPara;

		if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);		
		} else if (StringUtils.isEmpty(cdReqPara.getPage())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000009);	
		} else if (StringUtils.isEmpty(cdReqPara.getPagesize())){
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000010);				
		} else {
			flag = true;
		}

		return  flag;
	}
}
